package com.sinodata.ns.file;

import com.sinodata.ns.sv.ISupervisor;
import com.sinodata.ns.sv.SupervisorManager;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.utils.AnnexTool;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import org.limp.basework.SimpleBean;
import org.limp.basework.console.XSystemConfig;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.DateTrimmer;
import org.limp.mine.XMLTool;
import org.limp.mine.annex.AnnexInfo;

import java.io.File;

import java.sql.Connection;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 图像文件上传Action
 *
 * @author mido
 *
 */
public class FileUploadAction extends DispatchAction {
    private String REALPATH = (String) XSystemConfig.config().getProperty(XSystemConfig.XSYSTEM_REALPATH);

    /**
     * 图像文件上传
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward upload(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map params = cbh.getResource();

        String userid = (String) params.get("userid");

        Connection conn = null;

        ISupervisor sv = null;

        Map resultMap = new HashMap();
        resultMap.put("opera", "saveDoc");

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            SupervisorManager sm = new SupervisorManager(conn);

            sv = sm.getSupervisorByUserId(userid);
        } catch (Exception ex) {
            resultMap.put("status", "-1");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        if (sv != null) {
            try {
                cbh.getResource().put("username", sv.iuser().iname());
                cbh.getResource().put("groupid", sv.iuser().igroup().id());
                cbh.getResource().put("groupname", sv.iuser().igroup().iname());
                cbh.getResource().put("time", DateTrimmer.getYMDHMS());
                cbh.getResource().put("bdid",
                    (sv.ibd() != null) ? sv.ibd().id() : "0");
                cbh.getResource().put("bd",
                    (sv.ibd() != null) ? sv.ibd().iname() : "");
                cbh.getResource().put("gc",
                    (sv.ibd() != null) ? sv.ibd().ibdname() : "");

                SystemDataSource ds = SystemDataSource.getInstance();
                conn = ds.getConnection();

                BaseworkUtil util = new BaseworkUtil();

                SimpleBean bean = BaseworkUtil.analyzerSimpleBean("APP_NS_FILE",
                        cbh);

                Map result = (Map) util.manualSave("APP_NS_FILE", bean, conn);

                String id = (String) result.get("NF_ID");

                conn.commit();

                AnnexTool tool = new AnnexTool(conn);
                AnnexInfo[] annexs = tool.getAnnexs(id, "APP_NS_FILE");

                if ((annexs != null) && (annexs.length > 0)) {
                    CompressPicTool cpt = new CompressPicTool();

                    for (int i = 0; i < annexs.length; i++) {
                        if (CompressPicTool.validImg(annexs[i].getType())) {
                            File inputFile = new File(annexs[i].getPhysicalPath());
                            File outputFolder = new File(new File(REALPATH,
                                        "temp-store"), annexs[i].getPath());

                            if (!outputFolder.exists()) {
                                outputFolder.mkdirs();
                            }

                            File outputFile = new File(outputFolder,
                                    annexs[i].getTrueName());
                            outputFile.createNewFile();
                            cpt.compressPic(inputFile, outputFile, 64, 64, true);
                        }
                    }
                }

                resultMap.put("status", "0");
            } catch (Exception ex) {
                resultMap.put("status", "-1");
                conn.rollback();
                ISystem.catchException(request, this, ex);
            } finally {
                SystemDataSource.closeConnection(conn);
            }
        } else {
            resultMap.put("status", "-1");
        }

        String result = XMLTool.result2XML(resultMap);

        request.setAttribute("result", result);

        return mapping.findForward("result");
    }
}
